package leetcode;

/*
    进位相加即可
 */

import leetcode.entity.ListNode;

public class Code2 {


    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode result = new ListNode();
        ListNode curResult = result;
        int carry = 0;

        while (l1 != null || l2 != null || carry != 0) {
            if (l1 != null) {
                carry += l1.val;
                l1 = l1.next;
            }

            if (l2 != null) {
                carry += l2.val;
                l2 = l2.next;
            }

            curResult.val = carry % 10;
            carry = carry / 10;
            if(l1 != null || l2 != null || carry != 0) {
                curResult.next = new ListNode();
                curResult = curResult.next;
            }
        }
        return result;
    }

}